package com.example.lizhanqi.livevideo.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.lizhanqi.livevideo.R;
import com.example.lizhanqi.livevideo.bean.PhLoginBean;
import com.example.lizhanqi.livevideo.contacts.ContactAddress;
import com.example.lizhanqi.livevideo.presenter.PhLogin;
import com.example.lizhanqi.livevideo.view.IPhLoginUI;
import com.yolanda.nohttp.Logger;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.RequestQueue;

public class PHLoginActivity extends AppCompatActivity implements View.OnClickListener, IPhLoginUI {

    private EditText et_ph;
    private Button btn_getchecknumber;
    private EditText et_checknumber;
    private Button btn_login;
    private PhLoginBean phLoginBean;
    private PhLogin  phLogin;
    private RequestQueue requestQueue;
    int time = 60;
    Handler hd = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(time==0){
                btn_getchecknumber.setClickable(true);
                btn_getchecknumber.setText("重新发送");
            }else {
                btn_getchecknumber.setText(--time+"秒后重新发送");
                hd.sendEmptyMessageDelayed(time,1000);
                 time = 60;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phlogin);
        initView();
        phLogin = new PhLogin(this);
        phLoginBean = new PhLoginBean();
        requestQueue = NoHttp.newRequestQueue();
    }
    private void initView() {
        et_ph = (EditText) findViewById(R.id.et_ph);
        btn_getchecknumber = (Button) findViewById(R.id.btn_getchecknumber);
        et_checknumber = (EditText) findViewById(R.id.et_checknumber);
        btn_login = (Button) findViewById(R.id.btn_login);
        btn_getchecknumber.setOnClickListener(this);
        btn_login.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_getchecknumber:
                String string = et_ph.getText().toString().trim();
                if (!TextUtils.isEmpty(string)) {
                    phLoginBean.setPhone(et_ph.getText().toString());
                    if (phLogin.checkphone(phLoginBean)) {
                        phLogin.sendCode(ContactAddress.SENDCODE, phLoginBean, requestQueue, 69);
                    } else {
                        Toast.makeText(PHLoginActivity.this, "手机号格式错误", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(PHLoginActivity.this, "请输入手机号码", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_login:
                String  number= et_checknumber.getText().toString().trim();
                if (!TextUtils.isEmpty(number)) {
                       phLoginBean.setChecknumber(number);
                        phLogin.resqustLogin(ContactAddress.PHONELOING, phLoginBean, requestQueue,99);
                } else {
                    Toast.makeText(PHLoginActivity.this, "验证码不能为空", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
    @Override
    public void ok(int what,String msg) {
        if (what == 69) {
            Toast.makeText(PHLoginActivity.this, "短信发送成功", Toast.LENGTH_SHORT).show();
            btn_getchecknumber.setClickable(false);
            btn_getchecknumber.setText(time+"秒后重新发送");
            hd.sendEmptyMessage(time);
        }else if (what==99) {
            Logger.i("登录请求成功");
        }
    }
}
